package semspe;
import javax.swing.*;
import java.awt.event.*;

class AjoutStock extends JFrame implements ActionListener//implements WindowListener
{
	private JPanel panel;
	private JLabel numero;
	private JLabel nom;
	
	
	private JTextField txtNumero;
	private JTextField txtNom;

	
	private JOptionPane jop1;
	private JButton OK;
	private JButton annuler;
	
	public AjoutStock()
	{
		super("Ajouter un stock");
		this.setSize(400,600);
		this.setResizable(false);
		this.setVisible(true);
		
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.panel = new JPanel();
		//Numero Stock
		this.numero = new JLabel("Numero :");
		this.getContentPane().add(this.numero);
		this.numero.setBounds(30,20,150,20);
		this.txtNumero = new JTextField("");
		this.getContentPane().add(this.txtNumero);
		this.txtNumero.setBounds(200,20,150,20);
		
		//Nom Stock
		this.nom = new JLabel("Nom :");
		this.getContentPane().add(this.nom);
		this.nom.setBounds(30,60,150,20);
		this.txtNom = new JTextField("");
		this.getContentPane().add(this.txtNom);
		this.txtNom.setBounds(200,60,150,20);
		
		
		
				
		
		
		//BUT
		this.OK = new JButton("Ajouter");
		this.getContentPane().add(this.OK);
		this.OK.setBounds(100,400,100,20);
		this.annuler = new JButton("Annuler");
		this.getContentPane().add(this.annuler);
		this.annuler.setBounds(250,400,100,20);
		
		this.annuler.addActionListener(this);
		this.OK.addActionListener(this);
		
		jop1 = new JOptionPane();
		
		
	}
	

	public void actionPerformed(ActionEvent evt) {
			if(evt.getSource() == annuler)
			this.dispose();
			else if(evt.getSource() == OK){
                            if(testStock()==true){
			String requete = "INSERT INTO stock VALUES ("+this.txtNumero.getText()+", '"+this.txtNom.getText()+"')";
                           
                            
                            TraitRequete ajoutStock = new TraitRequete();
                            ajoutStock.ExecuteRequeteData(requete);
                            this.dispose();
                            }
                        }
	}                
        
        public boolean testStock(){
            if(this.txtNumero.getText().equals("")==false){
                if(this.txtNom.getText().equals("")==false){
                    
                    if(testInt(this.txtNumero.getText())==false){
                        jop1.showMessageDialog(null, "Numéro incorrect !");
                        return false;
                    }
                    
                    return true;
                }
            }
            jop1.showMessageDialog(null, "Des champs sont vides");
            return false;
        }
        
        public boolean testInt(String chaine) {
        try {
        int i = Integer.parseInt(chaine);
        }
        catch (Exception e) {
        return false;
        }
        return true;
        }
	
	public static void main (String[] a)
	{
		AjoutStock stock1 = new AjoutStock();
	}
	
} 
